plugin('msg', function () {
    var Msg = function () { 
        //this is msg plugin
    };
    Msg.prototype = {
        constructor: Msg,
        /**
         * 头部消息提醒
         */
        personal_msg_tips: function () {
            var msgTipText = page.g_msg_tip_text,
                spaceEle = $('[data-tooltips-model="my_space"]');
            if (msgTipText && spaceEle.length > 0) {
               
                var newToolTip = $("<div>").addClass("tooltip-box personal-tips");
                newToolTip.append(msgTipText);
                $("#site_header_tooltip").append(newToolTip);
            }
        },

        /**
         * 轮询空间消息提醒
         */
        polling: function (requestUrl) {
            if (!requestUrl) {
                return;
            }
            var _this = this;
            _this.personal_msg_tips();
            setInterval(function () {
                $.ajax({
                    url: page.polling_msg_tips_url,
                    method: "post",
                    dataType: "json",
                    data: "user_code=" + page.user_code,
                    success: function (result, status) {
                        if (status === "success") {
                            $(".personal-tips").remove();
                        }
                        if (result && result.totalMsgCount > 0) {
                            page.g_msg_tip_text = _this._buildMsgTipText(result);
                            _this.personal_msg_tips();
                        }
                    }
                });
            }, 1000 * 60 * 4); // 4分鐘取一次
        },
        /**
         * 组装空间消息提醒文本，根据各种消息的数量
         */
        _buildMsgTipText: function (tips) {
            if (tips.totalMsgCount <= 0) {
                return "";
            }
            var tpl = "<div id='MsgTip'>";
            if (tips.fansCount > 0) {
                tpl = tpl + page.g_msg_fans_tpl.replace("{{fansCount}}", tips.fansCount);
            }
            if (tips.privateMsgCount > 0) {
                tpl = tpl + page.g_msg_tpl.replace("{{privateMsgCount}}", tips.privateMsgCount);
            }
            if (tips.repliesCount > 0) {
                tpl = tpl + page.g_msg_reply_tpl.replace("{{repliesCount}}", tips.repliesCount);
            }
            if (tips.refMeCount > 0) {
                tpl = tpl + page.g_msg_ref_tpl.replace("{{refMeCount}}", tips.refMeCount);
            }
            tpl = tpl + "</div>";
            return tpl;
        }
    };
    return Msg;
});